<?php

/**
 * 页面访问控制
 *
 * @author etpasse
 * @package phone_server
 * @subpackage Access
 */
require_rewrite(WEB_INCLUDE_PATH . 'common.include.php');

class AccessModule {

    private $controller;
    private $method;

    /**
     * 初始化本类
     * @param str $controller
     * @param str $method
     */
    public function init($controller, $method) {
        $this->controller = $controller;
        $this->method = $method;
    }

    /**
     * 访问控制并显示输入验证码的页面
     *
     */
    public function check_access() {
        $blackCM = $this->blackCM();
        $if_search_agent = Handler_http::parseSpider();
        if (((!empty($blackCM[$this->controller]) && in_array($this->method, $blackCM[$this->controller])) ||
                (isset($blackCM[$this->controller]) && empty($blackCM[$this->controller])))
                && (!in_array(Handler_http::getIP(), $this->whiteIP()))
                && (!$if_search_agent)
        ) {
            $access = new access_control(APP_NAME . '-' . $this->controller . '-' . $this->method);
            if ($access->control_ip()) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 取得配置的要控制的类和方法
     * @return array
     */
    private function blackCM() {
        static $cm = NULL;
        if ($cm === NULL)
            $cm = require APP_PATH . 'config.access.php';

        return $cm;
    }

    /**
     * ip 白名单
     * @return array
     */
    private function whiteIP() {
        return array(
            '123.127.30.34',
            '123.127.30.35',
            '123.127.30.36',
            '123.127.30.37',
            '172.16.1.250',
            '172.16.1.251',
            '172.16.1.252',
            '172.16.1.253'
        );
    }

}

?>
